home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / v3_1 / sbp3_1e.lzh / LEARNER.PL < prev    next >
Text File  |  1991-11-01  |  2KB  |  63 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5. /* Modified for Quintus Prolog by Andreas Siebert */
  6.  
  7. /* LEARNER.PL */
  8. /* Program that modifies its knowledge base */
  9.  
  10. /* This program stores its knowledge base on the file KB.PL.
  11.    Initially, KB.PL should contain the clauses for capital_of
  12.    as given in CAPITALS.PL and a DYNAMIC statement. */
  13.  
  14. /* This program uses recursion to implement a loop. It will
  15.    run out of memory after a number of iterations. Techniques
  16.    to prevent this will be given in Chapter 4. */
  17.  
  18. start :-  consult('kb.pl'),
  19.       nl,
  20.       write('Note! Type names entirely in'),nl,
  21.       write('lower case, followed by a period.'),nl,
  22.       write('Type "stop." to quit.'),nl,
  23.       nl,
  24.       process_a_query.
  25.  
  26. process_a_query :- write('State? '),
  27.            read(State),
  28.            answer(State).
  29.  
  30.    /* If user typed "stop." then save the knowledge base and quit. */
  31.  
  32. answer(stop) :-    write('Saving the knowledge base...'),nl,
  33.            tell('kb.pl'),
  34.            listing(capital_of/2),
  35.            told,
  36.            write('Done.'),nl.
  37.  
  38.    /* If the state is in the knowledge base, display it, then
  39.       loop back to process_a_query */
  40.  
  41. answer(State) :-   capital_of(State,City),
  42.            write('The capital of '),
  43.            write(State),
  44.            write(' is '),
  45.            write(City),nl,
  46.            nl,
  47.            process_a_query.
  48.  
  49.    /* If the state is not in the knowledge base, ask the
  50.       user for information, add it to the knowledge base, and
  51.       loop back to process_a_query */
  52.  
  53. answer(State) :-   not capital_of(State,_),
  54.            write('I do not know the capital of that state.'),nl,
  55.            write('Please tell me.'),nl,
  56.            write('Capital? '),
  57.            read(City),
  58.            write('Thank you.'),nl,nl,
  59.            assertz(capital_of(State,City)),
  60.            process_a_query.
  61.  
  62.  
  63.